package com.roobo.hardware;

import android.text.TextUtils;
import android.util.Log;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

public class RockChipSn {
    private static final String TAG = "rk-sn";

    private String sn = null;
    private String wifi_mac = null;
    private String userData = null;

    // 20170306,add bsn for roobo.
    private String bsn = null;

    private static final String SN_PATH_STRING = "/sys/class/rk-sn/sn";
    private static final String USER_DATA_PATH_STRING = "/sys/class/rk-sn/userdata";
    private static final String BSN_PATH_STRING = "/sys/class/rk-sn/bsn";
    private static final String SYS_WIFI_MACADDR_PATH_STRING = "/sys/class/net/wlan0/address";
    private static RockChipSn instance;

    private RockChipSn() {
    }

    public static synchronized RockChipSn getInstance() {
        if (instance == null) {
            instance = new RockChipSn();
        }
        return instance;
    }

    private synchronized String readContent(String filePath) {
        File file = new File(filePath);
        BufferedReader reader = null;
        StringBuilder stringBuilder = new StringBuilder("");

        try {
            reader = new BufferedReader(new FileReader(file));
            String tempString = null;
            int line = 1;
            while ((tempString = reader.readLine()) != null) {
//            Log.e(TAG, "line " + line + ": " + tempString);
                line++;
                stringBuilder.append(tempString.replace(" ", "").replaceAll("\r|\n*", ""));
            }
            reader.close();
            Log.e(TAG, "stringBuilder " + stringBuilder.toString() );
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                }
            }
        }

        return stringBuilder.toString();
    }

    private void dumpDatas() {
        
        Log.d(TAG, "sn=" + getSN()
                + ",bsn=" + getBSN()
                + ",wifi_mac=" + getWifiMac()
                + ",userData=" + getUserData());
    }

    /**
     * 获取设备的序列号，也可以通过 系统属性 ro.serialno 获取
     * @return
     */
    public String getSN() {
        if (sn == null) {
            sn = readContent(SN_PATH_STRING);
        }
        
        return sn;
    }

    public String getWifiMac() {

        if (wifi_mac == null) {
            // wifi mac addr
            wifi_mac = readContent(SYS_WIFI_MACADDR_PATH_STRING);
            Log.d(TAG, "Wifi MAC:" + wifi_mac);
            if (wifi_mac != null) {
                if (wifi_mac.length() < 17) {
                    wifi_mac = null;
                } else if (wifi_mac.length() > 17) {
                    wifi_mac = wifi_mac.substring(0, 17).toUpperCase();
                }
            }
        }
        
        return wifi_mac;
    }

    public String getBSN() {
        if (bsn == null) {
            bsn = readContent(BSN_PATH_STRING);
        }
        return bsn;
    }


    public String getUserData() {
        if (userData == null) {
            userData = readContent(USER_DATA_PATH_STRING);
        }
        return userData;
    }


}
